const db = require('./db')

let filter = (req,res,next)=>{
    console.log(req.body)

    if(req.body.num==1){
        next()
    }else {
        return res.json({code:700,msg:'用户未登录'})
    }
}

module.exports = function (app) {
    app.all('*',function (req, res, next) {
        next();
    });

    app.get('/api/books/index', function (req, res) {   
    })
    
    app.post('/api/login',function(req,res){
        console.log('sdfds')
        if(req.body.username=='nimabi' && req.body.pwd=='123') {
            console.log('登录成功')
        
            req.session.username=req.body.username;
    
            return res.json({code:200,msg:'登录成功'})
        }else {
            console.log('玩蛇')
            return res.json({code:700,msg:'登录失败'})
        }

    })

    app.get('/api/logout',function(req,res){
        req.session.username = null;
        return res.json({code:200,msg:'退出登录成功'})
    })


    app.get('/api/buy',function(req,res){
        console.log(req.session.username)
        if(req.session.username){
            return res.json({code:200,msg:'已经登录'})
        }else {
            return res.json({code:700,msg:'没有登录'})
        }
    })
    

    app.post('/api/users',function(req,res){
        db.User.find({},function(err,docs){
            if(err){
                console.log('查询用户出错：'+err);
                res.json({code:700,msg:'查询出错'+err})
                return;
            } else {
                if(!docs) {
                    res.json({code:600,msg:'没有商品',data:docs})
                    return
                }
                else {
                    res.json({code:200,msg:'',data:docs})
                    return
                }
            }
        })
    })
}

